package com.xplay.xpocker.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ObjectUtils {
    
    public static boolean isEmpty(Object... objects) {
        return !notEmpty(objects);
    }
    
    /**
     * 此方法可以判断空内容
     *
     * @param objects
     * @return
     */
    
    public static boolean notEmpty(Object... objects) {
        boolean result;
        result = collectionNotEmpty(objects);
        if (!result) {
            return false;
        }
        for (Object object : objects) {
            if (!checkObjectNotEmpty(object)) {
                result = false;
                break;
            }
        }
        return result;
    }
    
    /**
     * 此方法仅仅判断空指针
     * <p>
     * 有一个空指针就返回ture
     *
     * @param objects
     * @return
     */
    
    public static boolean notNull(Object... objects) {
        boolean result = true;
        for (Object object : objects) {
            if (object == null) {
                result = false;
                break;
            }
        }
        return result;
    }
    
    
    private static boolean stringNotEmpty(String object) {
        return !object.equals("");
    }
    
    private static <T> boolean collectionNotEmpty(Collection<T> collection) {
        return collection != null && collection.size() > 0;
    }
    
    private static <T> boolean collectionNotEmpty(T[] collection) {
        return collection != null && collection.length > 0;
    }
    
    
    private static boolean checkObjectNotEmpty(Object object) {
        if (null == object) {
            return false;
        }
        if (object instanceof String) {
            return stringNotEmpty(object.toString());
        } else if (object instanceof List) {
            return collectionNotEmpty((Collection<?>) object);
        } else if (object instanceof Object[]) {
            return collectionNotEmpty((Object[]) object);
        } else if (object instanceof Map) {
            return !((Map<?, ?>) object).isEmpty();
        }
        return true;
    }
}
